var base = function() {
	return new Base();
}

function Base() { //构造函数

}
Base.prototype.moveLeft = function() //向左滑动产生的效果
{
	$('#homePage_hidden').hide(200);
	$('#content').animate({
		left: -$(window).width() * 0.75
	}, 200);
	if(!$('.wode').is(':hidden')) {
		$('.wode').css('display', 'none');
	}
}
Base.prototype.moveRight = function() //向右滑动产生的效果
{
	$('#homePage_hidden').show(200);
	$('#content').animate({
		left: 0
	}, 200);
	if(!$('.wode').is(':hidden')) {
		$('.wode').css('display', 'none');
	}
}
Base.prototype.zan = function() {
	$.post('php/dataAll.php', {
			user: $.cookie('username')
		},
		function(data, status) {
			var ojson = $.parseJSON(data);
			var div = $('.jianli').find('b');
			$.each(ojson, function(index, element) {
				var that = this;
				div.each(function() {
					if(that.id == $(this).html()) {
						$(this).parents('.jianli').find('.jianli_div_img').attr('num', that.num);
					}
				});
			})
		});
}
var touchmove = {
	div: $('#content'),
	startX: '',
	moveEndX: '',
	startY: '',
	moveEndY: '',
	start: function() {
		var that = this;
		this.div.on('touchstart', function(e) {
			//			e.preventDefault();
			that.startX = e.originalEvent.changedTouches[0].pageX;
			that.startY = e.originalEvent.changedTouches[0].pageY;
		});
	},
	end: function() {
		var that = this;
		this.div.on('touchend', function(e) {
			//			e.preventDefault();
			that.moveEndX = e.originalEvent.changedTouches[0].pageX;
			that.moveEndY = e.originalEvent.changedTouches[0].pageY;
			var X = that.moveEndX - that.startX;
			if(Math.abs(that.moveEndX - that.startX) > Math.abs(that.moveEndY - that.startY)) {
				if(X > 20) {
					base().moveRight();
				} else if(X < -20) {
					base().moveLeft();
				}
			}
		});
	},
	init: function() {
		this.start();
		this.end();
	}
};
var clickMove = {
	i: $("#liebiao"),
	div: touchmove.div,
	hidden_div: $('#homePage_hidden'),
	init: function() {
		var that = this;
		this.i.on('click', function() {
			base().moveRight();
		})

		this.hidden_div.on('click', function() {
			base().moveLeft();
		})
	}
}
var mainToggle = {
	div: $('.main_div_content_span'),
	toggle: function() {
		this.div.on('click', function() {
			$(this).children('span').slideToggle(200);
		})
	}
}
var liebiao = {
	main_div: $('.main'),
	div: $('.sidebar2_a'),
	run: function() {
		var that = this;
		this.div.on('click', function() {
			that.main_div.children().each(function() {
				if($(this).css('display') == 'block') {
					$(this).css('display', 'none');
				}
			});
			that.main_div.children().eq($(this).index()).css('display', "block");
			base().moveLeft();
			that.main_div.scrollTop('0');
		})
	}
}
$(function() {
	share().xinLoad();
	base().zan();
	touchmove.init(); //初始化触摸屏滑动
	clickMove.init(); //初始化点击滑动事件
	mainToggle.toggle();
	liebiao.run();
	share().lanLoad($('.jianli_div_img'), $('.main'));
	$('.icon-xihuan1').on('click', function() {
		share().xinRed($(this));
	});
	$('#foot_span1').on('click', function() {
		$('.main').children().each(function() {
			if($(this).css('display') == 'block') {
				$(this).css('display', 'none');
			}
		});
		$('.main').children().eq(2).css('display', 'block');
		$('.main').scrollTop('0');
	});
	$('.jianli_div_bottom_a').on('click', function(e) {
		e.preventDefault();
		xiazai($(this).attr('href'), $(this).siblings('span').children('b').html());
	   $('.tishi').css('display','block').html('正在下载');
	});
	$('.jianli_div_img').on('click', function() {
		share().clickImg($(this));
	});
	$('.fabiao_btn').on('click', function() {
		share().faBiao();
	});
	$('.pinglun').on('click', function() {
		$('.fabiao').slideToggle(100);
		if($(".pl_main").scrollTop() < 10) {
			$(".pl_main").animate({
				'scrollTop': 110
			}, 100);
		}
	});
	$('.icon-fanhui').on('click', function() {
		$('#content').css('display', 'block');
		$('#pinglun').css('display', 'none');
		$('textarea').val('');
		$('textarea').attr('placeholder', '宝贝满足你的期待吗？说说你的体验，分享与他人');
		$('.fabiao').css('display', 'none');
		$('.showpl').children('dl').remove();
		$('.dianzan_span').children('i').removeClass('red');
		var num = $('.dianzan_span').children('.num').html();
		var b = $('.pl_main_2_top_p1').attr('b');
		if(num != '') {
			$('.jianli b').each(function(index, element) {
				if($(this).html() === b) {
					$(this).parents('.jianli').find('.jianli_div_img').attr('num', num);
				}
			})
		}
	})
	$('.dianzan_span').on('click', function() {
		var that = this;
		share().dianzan(this);
		$.post('php/xiugaidata.php', {
				id: $('.pl_main_2_top_p1').attr('b'),
				biao: 'muban',
				num: $(that).children('.num').html()
			},
			function(data, status) {});
	});
	$('.sidebar3').children('a').on('click', function() {
		$.cookie('username');
		plus.runtime.quit();
	})
	document.addEventListener('plusready', function() {
		var i = 0;
		//console.log("所有plus api都应该在此事件发生后调用，否则会出现plus is undefined。"
		plus.key.addEventListener('backbutton', function() {
			//					plus.runtime.quit();
			if($('#content').is(':hidden')) {
				i=0;
				$('#content').css('display', 'block');
				$('#pinglun').css('display', 'none');
			} else {
			    i++;
				if(i == 2) {
					plus.runtime.quit();
				} else {
					$('.tishi').html('再点一次退出');
					$('.tishi').css('display','block');
	                setTimeout(function(){
						$('.tishi').css('display','none');
					},800);
				}
			}
		});
	});
})